<?php
namespace addons\fzdc\job;

use app\admin\model\fzdc\coupon\Coupon;
use app\admin\model\fzdc\coupon\Receive;
use app\admin\model\fzdc\order\Cart;
use app\admin\model\fzdc\order\Order;
use app\admin\model\fzdc\goods\SkuProduct;
use app\admin\model\fzdc\order\Detail;

use app\admin\model\fzdc\store\Sear;
use think\Db;
use think\Log;
use think\queue\Job;

/**
 * 用于处理超时订单自动取消
 */
class Overtime
{
    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        Log::log("进入队列:");
        //执行业务处理
        if($this->doJob($data)){
            $job->delete();//任务执行成功后删除
            Log::log("dismiss job has been down and deleted");
        }else{
            //检查任务重试次数
            if($job->attempts() > 3){
                Log::log("dismiss job has been retried more that 3 times");
                $job->delete();
                Db::close();
            }else{
                $job->release(2);
            }
        }
    }

    /**
     * 根据消息中的数据进行实际的业务处理
     */
    private function doJob($data)
    {
        // 实际业务流程处理
        //查询该订单是否已付款
        if (empty($data)){
            return true;
        }
        if ($data['type']==1 || $data['type']==2){
            $orderInfo = Order::get(['order_no'=>$data['orderNo']]);
            if (!$orderInfo){
                Log::log("数据为空删除队列：".$data['orderNo']);
                // $job->delete();
                Db::close();
                return true;
            }
        }

        if ($data['type']==1){
            if ($orderInfo->status != 1){
                //订单已付款
                Log::log("该订单已支付".$data['orderNo']);
                Db::close();
                return true;
            }else{
                if ($orderInfo->dc_type == 2){
                    Db::close();
                    return true;
                }
                Log::log("该订单未支付".$data['orderNo']);
                Db::startTrans();
                try {
                    //订单已超时 更新订单状态
                    $res = Order::where(['order_no'=>$data['orderNo']])->update(['status'=>5]);
                    if ($orderInfo->seat_id){
                        Sear::whereIn("id",$orderInfo->seat_id)->update(["status"=>1]);
                    }
                    Log::log("订单更新状态".$res);
                    //清除购物车数据
                    Cart::where("id","in",$orderInfo->cart_ids)->delete();
                    //判断是否使用优惠券
                    if ($orderInfo->coupon_id > 0){
                        //更新优惠券为未使用，判断优惠券是否过期
                        $coupon = Coupon::get($orderInfo->coupon_id);
                        if (time() > $coupon->end_time){
                            $res5 = Receive::where(["user_id"=>$orderInfo->user_id,"coupon_id"=>$orderInfo->coupon_id])->update(['state'=>-1]);
                        }else{
                            $res5 = Receive::where(["user_id"=>$orderInfo->user_id,"coupon_id"=>$orderInfo->coupon_id])->update(['state'=>0]);
                        }
                        Log::log("优惠券更新状态".$res5);
                    }
                    //增加库存
                    if ($orderInfo->cart_ids){
                        $ids = Detail::where(['order_id'=>$orderInfo['id']])->column('id');
                        foreach ($ids as $v){
                            $detail  = Detail::where(["id"=>$v])->find();
                            $sku = SkuProduct::get(["key"=>$detail['goods_item_id']]);
                            $sku->setInc("stock",$detail['goods_count']);
                        }
                    }
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    Log::log($e->getMessage());
                    Db::close();
                    // $job->delete();
                    return true;
                }
                Db::close();
                return true;
            }
        }elseif ($data['type']==2){
            $orderInfo->status = 3;
            $orderInfo->save();
            Db::close();
            return true;
        }elseif ($data['type']==3){
            Log::log("开始释放桌号".$data['seat_id']);
            Sear::whereIn("id",$data['seat_id'])->update(["use_status"=>1]);
            Db::close();
            return true;
        }else{
            $orderInfo = Order::get(['order_no'=>$data['orderNo']]);
            Sear::whereIn("id",$orderInfo->seat_id)->update(["use_status"=>2]);
            Db::close();
            return true;
        }

    }
}